﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;

namespace FootBallMatchProject.Piography.Admin
{
    public partial class AddEditPlayer : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            DataClassLayer.Player player = new DataClassLayer.Player();
            DataClassLayer.PlayerCollection players = new DataClassLayer.PlayerCollection();
            if (!IsPostBack)
            {
                RefreshNationalitys();
                RefreshClubs();
                RefreshNationalTeams();
                if (Request.QueryString["operation"] != null &&
                    Request.QueryString["operation"].ToString() == "Edit")
                {
                    
                    if (Request.QueryString["id"] != null &&
                    Request.QueryString["id"].ToString() != "")
                    {
                        player.ID = int.Parse(Request.QueryString["id"].ToString());
                        DataTable table = players.FillList(player);
                        if (table.Rows.Count == 1)
                        {
                            btnClear.Visible = false;
                            txtPlayer.Text = table.Rows[0]["Name"].ToString();
                            dtDateOfBirth.Text = table.Rows[0]["BirthDate"].ToString();
                            cmbNationalitys.SelectedValue = table.Rows[0]["HomeTown"].ToString();
                            txtWebSite.Text = table.Rows[0]["OfficialWebSite"].ToString();
                            cmbClubs.SelectedValue = table.Rows[0]["ClubID"].ToString();
                            cmbNationalTeams.SelectedValue = table.Rows[0]["NationalTeamID"].ToString();
                            txtGeneralInformation.Text = table.Rows[0]["GeneralInformation"].ToString();
                            PlayerImage.ImageUrl = "~/images/Player_Images/" + Request.QueryString["id"].ToString() + "/" + table.Rows[0]["Image"].ToString();
                            PlayerImage.Visible = true;
                            hfPlayerImage.Value = table.Rows[0]["Image"].ToString();
                            txtPlaceOfBirth.Text = table.Rows[0]["PlaceOfBirth"].ToString();
                            txtZodiaq.Text = table.Rows[0]["Zo_diaq"].ToString();

                        }
                    }
                }
            }
        }
        private void RefreshNationalitys()
        {
            DataClassLayer.Country country = new DataClassLayer.Country();
            DataClassLayer.CountryCollection countryes = new DataClassLayer.CountryCollection();
            DataTable table = countryes.FillList(country);
            if (table.Rows.Count > 0)
            {
                cmbNationalitys.DataSource = table;
                cmbNationalitys.DataValueField = "ID";
                cmbNationalitys.DataTextField = "Description";
                cmbNationalitys.DataBind();
                cmbNationalitys.Items.Insert(0, new ListItem("-اختيار-"));
            }
        }
        private void RefreshClubs()
        {
            DataClassLayer.Club club = new DataClassLayer.Club();
            DataClassLayer.ClubCollection clubs = new DataClassLayer.ClubCollection();
            DataTable table = clubs.FillList(club);
            if (table.Rows.Count > 0)
            {
                cmbClubs.DataSource = table;
                cmbClubs.DataValueField = "ID";
                cmbClubs.DataTextField = "Name";
                cmbClubs.DataBind();
                cmbClubs.Items.Insert(0,new ListItem("-اختيار-"));
            }
        }
        private void RefreshNationalTeams()
        {
            DataClassLayer.NationalTeam nationalteam = new DataClassLayer.NationalTeam();
            DataClassLayer.NationalTeamCollection nationalteams = new DataClassLayer.NationalTeamCollection();
            DataTable table = nationalteams.FillList(nationalteam);
            if (table.Rows.Count > 0)
            {
                cmbNationalTeams.DataSource = table;
                cmbNationalTeams.DataValueField = "ID";
                cmbNationalTeams.DataTextField = "Name";
                cmbNationalTeams.DataBind();
                cmbNationalTeams.Items.Insert(0, new ListItem("-اختيار-"));
            }
        }

        protected void btnClose_Click(object sender, EventArgs e)
        {
            Response.Redirect("PlayerList.aspx");
        }

        protected void btnClear_Click(object sender, EventArgs e)
        {
            Response.Redirect(Request.RawUrl);
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            DataClassLayer.Player player = new DataClassLayer.Player();
            DataClassLayer.PlayerCollection players = new DataClassLayer.PlayerCollection();
            player.Name = txtPlayer.Text;
            player.BirthDate = DateTime.Parse(dtDateOfBirth.Text);
            if (cmbNationalitys.SelectedIndex != 0 &&
                cmbNationalitys.SelectedIndex != -1)
            {
                player.HomeTown = int.Parse(cmbNationalitys.SelectedValue.ToString());
            }
            else
            {
                ///pop msg
                return;
            }
            player.Image = hfPlayerImage.Value;
            player.OfficialWebSite = txtWebSite.Text;
            if (cmbClubs.SelectedIndex != 0 &&
               cmbClubs.SelectedIndex != -1)
            {
                player.ClubID = int.Parse(cmbClubs.SelectedValue.ToString());
            }
            else
            {
                ///pop msg
                return;
            }
            player.GeneralInformation = txtGeneralInformation.Text;
            if (cmbNationalTeams.SelectedIndex != 0 &&
               cmbNationalTeams.SelectedIndex != -1)
            {
                player.NationalTeamID = int.Parse(cmbNationalTeams.SelectedValue.ToString());
            }
            else
            {
                ///pop msg
                return;
            }
            player.PlaceOfBirth = txtPlaceOfBirth.Text;
            player.Zodiaq = txtZodiaq.Text;
            if (Request.QueryString["operation"] != null &&
                Request.QueryString["operation"].ToString() == "Edit")
            {
                if (Request.QueryString["id"] != null &&
                              Request.QueryString["id"].ToString() != "")
                {
                    player.ID = int.Parse(Request.QueryString["id"].ToString());
                    bool chkvalue = players.Edit(player);
                    if (chkvalue)
                    {
                        if (!System.IO.Directory.Exists(HttpContext.Current.Server.MapPath("~/images/Player_Images/" + GetID())))
                        {
                            System.IO.Directory.CreateDirectory(HttpContext.Current.Server.MapPath("~/images/Player_Images/" + GetID()));
                        }
                        string fileName = null;
                        string[] fileArray = Directory.GetFiles(HttpContext.Current.Server.MapPath("~/images/Temp/Player_Images"));
                        for (int i = 0; i < fileArray.Length; i++)
                        {
                            fileName = Path.GetFileName(fileArray[i]);
                            File.Copy(fileArray[i], HttpContext.Current.Server.MapPath("~/images/Player_Images/" + GetID() + "/" + fileName), true);
                        }
                        Response.Redirect("PlayerList.aspx");
                    }
                    else
                    {
                        ///pop msg
                    }
                }
                else
                {
                    ///pop msg
                }
            }
            else
            {
                bool chkvalue = players.Add(player);
                if (chkvalue)
                {
                    if (!System.IO.Directory.Exists(HttpContext.Current.Server.MapPath("~/images/Player_Images/" + GetID())))
                    {
                        System.IO.Directory.CreateDirectory(HttpContext.Current.Server.MapPath("~/images/Player_Images/" + GetID()));
                    }
                    string fileName = null;
                    string[] fileArray = Directory.GetFiles(HttpContext.Current.Server.MapPath("~/images/Temp/Player_Images"));
                    for (int i = 0; i < fileArray.Length; i++)
                    {
                        fileName = Path.GetFileName(fileArray[i]);
                        File.Copy(fileArray[i], HttpContext.Current.Server.MapPath("~/images/Player_Images/" + GetID() + "/" + fileName), true);
                    }
                    Response.Redirect("PlayerList.aspx");
                }
                else
                {

                    ///pop msg
                }
            }
        }

        protected void btnUploadPlayerImage_Click(object sender, EventArgs e)
        {
            string Thump_Name_File = "";
            DataAccessObject.StaticClass.UploadData(ref PlayerImage, "~/images/temp/Player_Images/", UploadPlayerImage, ref Thump_Name_File);
            hfPlayerImage.Value = Thump_Name_File;
            PlayerImage.Visible = true;
        }
        private string GetID()
        {
            if (Request.QueryString["operation"] != null &&
                Request.QueryString["operation"].ToString() == "Edit")
            {
                if (Request.QueryString["id"] != null &&
                Request.QueryString["id"].ToString() != "")
                {
                    return Request.QueryString["id"].ToString();
                }
                else
                    return "";
            }
            else
            {
                DataClassLayer.Player player = new DataClassLayer.Player();
                DataClassLayer.PlayerCollection players = new DataClassLayer.PlayerCollection();
                return players.MaxID().ToString();
            }
        }
    }
}